/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if (l1 == NULL && l2 == NULL) {
            return NULL;
        }

        ListNode *head = NULL;
        ListNode *cur = NULL;
        int carry = 0;

        while (l1 || l2 || carry) {
            if (l1) {
                carry += l1->val;
                l1 = l1->next;
            }
            if (l2) {
                carry += l2->val;
                l2 = l2->next;
            }
            ListNode *node = new ListNode(carry % 10);
            carry /= 10;

            if (head == NULL) {
                head = node;
                cur = node;
            } else {
                cur->next = node;
                cur = cur->next;
            }
        }
        return head;

    }
};
